#include "StateManager.h"
#include "Context.h"
#include "Logger.h"

namespace BPL
{
StateManager::StateManager()
{

}

StateManager::~StateManager()
{
	mpt_parent = NULL;

	while(!mpt_ActiveStateStack.empty())
    {
        mpt_ActiveStateStack.back()->exit();
        mpt_ActiveStateStack.pop_back();
    }

    Logger::log("delete stateManager");
}

void StateManager::update(float dt)
{
	if(!mpt_ActiveStateStack.empty())
        mpt_ActiveStateStack.back()->update(dt);
}

void StateManager::changeState(const StatePtr& state)
{
	if(!mpt_ActiveStateStack.empty())
    {
		mpt_ActiveStateStack.back()->exit();
        mpt_ActiveStateStack.pop_back();
    }

	mpt_ActiveStateStack.push_back(state);
    mpt_ActiveStateStack.back()->setParent(this);
    mpt_ActiveStateStack.back()->enter();
}

bool StateManager::pushState(const StatePtr& state)
{
	if(!mpt_ActiveStateStack.empty())
    {
		if(!mpt_ActiveStateStack.back()->pause())
			return false;
    }

    mpt_ActiveStateStack.push_back(state);
    mpt_ActiveStateStack.back()->setParent(this);
    mpt_ActiveStateStack.back()->enter();

	return true;
}

void StateManager::popState()
{
	if(!mpt_ActiveStateStack.empty())
    {
		mpt_ActiveStateStack.back()->exit();
		mpt_ActiveStateStack.pop_back();
    }

	if(!mpt_ActiveStateStack.empty())
	{
		mpt_ActiveStateStack.back()->resume();
    }
}

void StateManager::popAllStates()
{
	while (!mpt_ActiveStateStack.empty())
    {
        mpt_ActiveStateStack.back()->exit();
        mpt_ActiveStateStack.pop_back();
    }
}

}
